//
//  ScopeConverViewController.m
//  JxCarios
//
//  Created by xcq on 15/3/3.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopeConverViewController.h"
#import "CarAroundDataSource.h"
#import "QrCodeViewController.h"
#import "OnlinePayTableViewCell.h"
#import "TkAndOilCardView.h"

#define INTCONVERSTR(value)     [NSString stringWithFormat:@"%d",value];

// 积分兑换和当面付订单使用系统自带的cell
// 线上支付订单   自定义cell
@interface ScopeConverViewController ()
{
    CarAroundDataSource *carAround;
}
@end

@implementation ScopeConverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _currenPage = 1;
    [self initAbout];
    [self netRequst];
}

- (void)initAbout {
    if (_type == ScoreConverValue) {  //积分兑换
        _navStr = @"积分兑换";
        self.title = [@"积分兑换" stringByAppendingString:@"订单"];
    }else if (_type == OfflineValue)  { //当面付
        _navStr = @"当面付";

        self.title = [@"当面付" stringByAppendingString:@"订单"];
    }else{
        [self.tableView registerNib:[UINib nibWithNibName:@"OnlinePayTableViewCell" bundle:nil] forCellReuseIdentifier:cellPay];
        _navStr = @"线上支付";

        self.title = [@"线上支付" stringByAppendingString:@"订单"];
    }
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefrshAction)];
}

- (void)footerRefrshAction {
    if ([_totalPage integerValue] <= _currenPage) {
        [self.view.window makeToast:@"没有更多数据了哦"];
        [self.tableView footerEndRefreshing];
        return;
    }
    _currenPage ++;
    carAround.dic_type = INTCONVERSTR(_type);
    carAround.page =  INTCONVERSTR(_currenPage);
    [carAround getOrderDetail:^(id result) {
        [_tableDataSource addObjectsFromArray:[NSMutableArray arrayWithArray:[result customObjectForkey:@"orderlist"]]];
        [self.tableView footerEndRefreshing];

        [self.tableView reloadData];
    } anfFail:^(id result) {

        _currenPage --;
        [self.tableView footerEndRefreshing];

    }];
}

- (void)netRequst {
    if (!carAround) {
        carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    carAround.dic_type = INTCONVERSTR(_type);
    carAround.page =  INTCONVERSTR(_currenPage);
    [carAround getOrderDetail:^(id result) {
        _tableDataSource = [NSMutableArray arrayWithArray:[result customObjectForkey:@"orderlist"]];
        _totalPage = [result customObjectForkey:@"totalpages"];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
    
}

- (void)initDataSource {
    if (_tableDataSource.count <= 0) {
        return;
    }
    if (!self.tableView.delegate) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    [self.tableView reloadData];
}

#pragma mark - tableView delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableDataSource.count == 0 ? 0 : _tableDataSource.count;
}


static NSString *cell1 = @"score";
static NSString *cellPay = @"onlinePay";

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_type == lineValue) { //线上支付
        static NSString *cellPay = @"onlinePay";

        OnlinePayTableViewCell *online = [tableView dequeueReusableCellWithIdentifier:cellPay forIndexPath:indexPath];
        online.shopName.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"GOOD_NAME"];
        online.price.text = [[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_PRICE"] stringByAppendingString:@"元"];
        if ([[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"USE_POINT"] integerValue] > 0) {
            online.userScope.hidden = NO;
            online.userScope.text = @"已使用积分抵扣";
        }
        
        return online;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell1];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell1];
        }
        cell.textLabel.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"GOOD_NAME"];

        if (_type == ScoreConverValue){
            cell.detailTextLabel.text = @"积分兑换";
        }else{
            NSString *pre = @"当面付";
            cell.detailTextLabel.text = [[pre stringByAppendingString:[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"ORDER_PRICE"]] stringByAppendingString:@"元"] ;
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QrCodeViewController *qr = [[QrCodeViewController alloc] initWithNibName:@"QrCodeViewController" bundle:nil];
    qr.info = [_tableDataSource objectAtIndex:indexPath.row];
    qr.barTitle = _navStr;
    qr.type = _type;
    [self.navigationController pushViewController:qr animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
